package com.example.springBoot.controller;

import com.example.springBoot.domain.Girl;
import com.example.springBoot.service.GirlService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

/**
 * @author chenpeng
 * @date 2018/4/18 17:36
 */
@RestController
public class GirlController {
    @Autowired
    private GirlRepository girlRepository;

    @Autowired
    private GirlService girlService;

    /**
     * 查询所有女生列表
     * @return
     */
    @GetMapping("/girls")
    public List<Girl> girlList(){
        return girlRepository.findAll();
    }
    /**
     * 添加一个女生
     */
    @PostMapping(value = "/girls")
    public Girl girlAdd(
            @RequestParam("cupSize") String cupSize,
            @RequestParam("age") Integer age
    ){
        Girl girl = new Girl();
        girl.setCupSize(cupSize);
        girl.setAge(age);
        return girlRepository.save(girl);
    }
    /**
     * 通过id查询女生
     */
    @GetMapping("/get/{id}")
    public Optional<Girl> girlGet(@PathVariable(value = "id") int id){
        return girlRepository.findById(id);
    }
    /**
     * 通过id更新一个女生
     */
    @PutMapping("/update/{id}")
    public Girl update(@PathVariable(value = "id") int id,
                       @RequestParam(value = "cupSize") String cupSize,
                       @RequestParam(value = "age") int age
    ){
        Girl girl = new Girl();
        girl.setId(id);
        girl.setCupSize(cupSize);
        girl.setAge(age);
        return girlRepository.save(girl);
    }
    /**
     * 产出一个女生
     */
    @DeleteMapping(value = "/dele/{id}")
    public void girlDelete(@PathVariable(value = "id") int id){
        girlRepository.deleteById(id);
    }

    @PostMapping(value = "/girls/two")
    public void girlTwo(){
        girlService.insertTwo();
    }
}
